"""
基于哈希值和线性Map 实现哈希Map
author: Mr_52Hz
date: 2019-11-09
"""
from LinkMap import LinkMap


class HashMap(object):

    def __init__(self, size=100):
        # 初始化maps
        self.maps = [LinkMap() for _ in range(size)]

    def find_link_map(self, key):
        key_hash_value = hash(key) % len(self.maps)
        return self.maps[key_hash_value]

    def add(self, key, value):
        link_map_index = self.find_link_map(key)
        link_map_index.add(key, value)

    def get(self, key):
        link_map_index = self.find_link_map(key)
        return link_map_index.get(key)


class Map(object):

    def __init__(self):
        self.maps = HashMap(2)
        self.nums = 0

    def add(self, key, value):
        link_map = self.maps.find_link_map(key)
        link_map.add(key, value)
        self.nums += 1

    def get(self, key):
        link_map = self.maps.find_link_map(key)
        link_map.get(key)

    def rebuild(self):
        if self.nums == len(self.maps.maps):
            pass


if __name__ == '__main__':
    hm = HashMap()
    hm.add('name', 'laowang')
    print(hm.get('name'))


